import random;
import pygame;


# 游戏窗口尺寸
WINDOW_RECT = pygame.Rect(0,0,512,768);

# 背景数组
BG_LIST = ["../images/img_bg_level_1.jpg","../images/img_bg_level_2.jpg","../images/img_bg_level_3.jpg",
           "../images/img_bg_level_4.jpg","../images/img_bg_level_5.jpg"];

# 敌人数组
ENEMY_LIST = ["../images/img-plane_1.png","../images/img-plane_2.png","../images/img-plane_3.png",
              "../images/img-plane_4.png","../images/img-plane_5.png","../images/img-plane_6.png","../images/img-plane_7.png"];

# 英雄数组
MAN_LIST = ["../images/hero.png","../images/hero2.png"];

# 子弹
BULLET_LIST = ["../images/hero_bullet_1.png","../images/hero_bullet_2.png","../images/hero_bullet_3.png","../images/hero_bullet_4.png",
              "../images/hero_bullet_5.png","../images/hero_bullet_6.png","../images/hero_bullet_7.png"];

# 创建敌人定时器事件
CREATE_ENEMY_EVENT = pygame.USEREVENT;

# 发射子弹定时器事件
FIRE_BULLET_EVENT = pygame.USEREVENT + 1;

# 背景音乐
BG_MUSI_PATH = "../musi/bg.wav";

# 飞机大战 精灵
class PlaneSprite(pygame.sprite.Sprite):

    # image,rect 位置,speed 速度
    def __init__(self,image_path,speed=1):
        super().__init__()

        self.image = pygame.image.load(image_path);
        self.rect = self.image.get_rect();
        self.speed = speed;


    def update(self):
        self.rect.y += self.speed;


# 背景精灵
class Background(PlaneSprite):
    # 游戏背景精灵
    def update(self):
        super().update();

        if( self.rect.y >= WINDOW_RECT.height):
            self.rect.y = -WINDOW_RECT.height;

# 英雄精灵
class Man(PlaneSprite):

    def __init__(self):

        man_index = random.randint(0,len(MAN_LIST)-1); # 当前英雄皮肤索引
        super().__init__(MAN_LIST[man_index],0);

        # X轴居中
        # man.rect.x = (self.window.get_width() - man.rect.width) / 2;
        self.rect.centerx = WINDOW_RECT.centerx;

        # Y轴最底，向上偏移50
        # self.rect.y = self.window.get_height() - man.rect.height - 50;
        self.rect.y = WINDOW_RECT.bottom - 120;


        self.bullet_group = pygame.sprite.Group();


    def update(self):
        self.rect.x += self.speed;

        if self.rect.x <= 0:
            self.rect.x = 0;
        elif self.rect.x >= (WINDOW_RECT.width - self.rect.width):
            # print(self.rect.x)
            self.rect.x = WINDOW_RECT.width - self.rect.width;

    # 发射子弹
    def fire(self):
        # 一次发射三枚
        # for i in 1:
            i = 1;
            bullte = Bullet();
            bullte.rect.centerx = self.rect.centerx;
            bullte.rect.bottom = self.rect.y - bullte.rect.height *i;
            self.bullet_group.add(bullte);


# 敌人精灵
class Enemy(PlaneSprite):

    # 敌人数量统计
    enemy_num = 0;

    # 被击落敌人数量统计
    ko_enemy_num = 0;

    def __init__(self):

        # 当前敌人皮肤索引
        enemy_index = random.randint(0,len(ENEMY_LIST)-1);

        super().__init__(ENEMY_LIST[enemy_index]);

        # 速度
        self.speed = random.randint(2,6);

        # 等同于 敌人高度得负值 即：self.rect.y = -self.rect.height;
        self.rect.bottom = 0;
        max = WINDOW_RECT.width - self.rect.width;
        self.rect.x = random.randint(0,max);

        Enemy.enemy_num += 1;
        # print(f"创建一个敌人,当前敌人数量{Enemy.enemy_num}");


    def update(self):
        super().update();

        if self.rect.y > WINDOW_RECT.height:
            # 从精灵组中移除
            self.kill();


    def __del__(self):
        # print("飞过界一个敌人...")
        pass;


# 子弹精灵
class Bullet(PlaneSprite):

    def __init__(self):
        super().__init__(BULLET_LIST[1],-2);

    def update(self):
        super().update();

        if self.rect.bottom < 0:
            self.kill();

    def __del__(self):
        # print("让子弹飞一会...");
        pass;
